﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EasyStudy.Logic;
using EasyStudy.Properties;
using ComponentFactory.Krypton.Toolkit;

namespace EasyStudy.UI
{
    public partial class FrmShowUnfinishedTasks : KryptonForm
    {

        private void loadDataSource()
        {
            DataTable dt = EbbTaskDBMgr.GetUnfinishedTasksAWeekToDataTable();
            for (int i = 0; i < dt.Columns.Count; )
            {
                string colName = dt.Columns[i].ColumnName;
                switch (colName.ToUpper())
                {
                    case "NEXT_INDEX":
                        dt.Columns.RemoveAt(i);
                        break;
                    case "REMIND_TIME_LIST":
                        dt.Columns.RemoveAt(i);
                        break;
                    case "CREATE_TIME":
                        dt.Columns.RemoveAt(i);
                        break;
                    case "LAST_UPDATE_TIME":
                        dt.Columns.RemoveAt(i);
                        break;
                    default:
                        ++i;
                        break;
                }
            }
            DataView dv = new DataView(dt);
            dv.AllowDelete = false;
            dv.AllowEdit = false;
            dv.AllowNew = false;

            this.dataGridView.DataSource = dv;
            for (int i = 0; i < dt.Columns.Count; ++i)
            {
                this.dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            this.dataGridView.Columns[0].FillWeight = 10;
            this.dataGridView.Columns[1].FillWeight = 20;
            this.dataGridView.Columns[2].FillWeight = 30;
            this.dataGridView.Columns[3].FillWeight = 20;
            this.dataGridView.Columns[3].FillWeight = 20;
        }

        public FrmShowUnfinishedTasks()
        {
            InitializeComponent();
            this.dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            loadDataSource();
            //DataTable dt = EbbTaskDBMgr.GetUnfinishedTasksAWeekToDataTable();
            //for (int i = 0; i < dt.Columns.Count; )
            //{
            //    string colName = dt.Columns[i].ColumnName;
            //    switch(colName.ToUpper())
            //    {
            //        case "NEXT_INDEX":
            //            dt.Columns.RemoveAt(i);
            //            break;
            //        case "REMIND_TIME_LIST":
            //            dt.Columns.RemoveAt(i);
            //            break;
            //        case "CREATE_TIME":
            //            dt.Columns.RemoveAt(i);
            //            break;
            //        case "LAST_UPDATE_TIME":
            //            dt.Columns.RemoveAt(i);
            //            break;
            //        default:
            //            ++i;
            //            break;
            //    }
            //}
            //DataView dv = new DataView(dt);
            //dv.AllowDelete = false;
            //dv.AllowEdit = false;
            //dv.AllowNew = false;

            //this.dataGridView.DataSource = dv;


            Icon = Resources.task1;
        }

        private void ToolStripMenuItemCopy_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows[0] == null)
            {
                return;
            }

            string subject = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
            string comment = dataGridView.SelectedRows[0].Cells[2].Value.ToString();

            StringBuilder sBuilder = new StringBuilder();
            sBuilder.AppendLine("主题:" + subject);
            sBuilder.AppendLine("描述:" + comment);
            Clipboard.SetText(sBuilder.ToString());
            MessageBox.Show("已复制到剪切板\r\n" + sBuilder.ToString());
        }

        private void ToolStripMenuItemEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows != null && dataGridView.SelectedRows.Count == 1)
            {
                EbbinghausTask task = new EbbinghausTask();

                DataGridViewRow row = dataGridView.SelectedRows[0];
                task.ID = row.Cells[0].Value.ToString();
                task.Type = row.Cells[1].Value.ToString();
                task.Subject = row.Cells[2].Value.ToString();
                task.Comment = row.Cells[3].Value.ToString();

                FrmUpdateEbbTask frmUpdateTask = new FrmUpdateEbbTask(task);
                DialogResult diaRes = frmUpdateTask.ShowDialog(this);
                if (diaRes == System.Windows.Forms.DialogResult.OK)
                {
                    loadDataSource();
                }
            }
        }

        private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                //若行已是选中状态就不再进行设置
                if (e.RowIndex < 0 || e.RowIndex > dataGridView.Rows.Count - 1)
                {
                    return;
                }

                if (dataGridView.Rows[e.RowIndex].Selected == false)
                {
                    dataGridView.ClearSelection();
                    dataGridView.Rows[e.RowIndex].Selected = true;
                }
                //只选中一行时设置活动单元格
                if (dataGridView.SelectedRows.Count == 1)
                {
                    dataGridView.CurrentCell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
                //弹出操作菜单
                contextMenuStripCell.Show(MousePosition.X, MousePosition.Y);
            }
        }

        private void ToolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows != null && dataGridView.SelectedRows.Count == 1)
            {
                DataGridViewRow row = dataGridView.SelectedRows[0];
                string id = row.Cells[0].Value.ToString();

                msgBox.Content = "学习是一项需要长期坚持的事情，您已经确认好了要删除该任务了吗？";
                msgBox.WindowTitle = "删除任务";
                msgBox.CommonButtons = TaskDialogButtons.OK | TaskDialogButtons.Cancel;

                DialogResult dres = msgBox.ShowDialog(this);
                if (dres == System.Windows.Forms.DialogResult.OK)
                {
                    EbbTaskDBMgr.DeleteATask(id);
                    loadDataSource();
                }
            }
        }
    }
}
